package com.zhangc.blog.admin.web.controller.admin;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangc.blog.admin.entity.BlogTagEntity;
import com.zhangc.blog.admin.service.BlogTagServiceImpl;
import com.zhangc.blog.admin.web.dto.AjaxPutPage;
import com.zhangc.blog.admin.web.dto.AjaxResultPage;
import com.zhangc.blog.base.ResultBO;
import com.zhangc.blog.base.enums.DeleteStatusEnum;

/**
 * @qq交流群 796794009
 * @qq 1320291471
 * @Description: 标签Controller
 * @create 2020/12/27
 */
@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private BlogTagServiceImpl blogTagService;

    @GetMapping("/v1/tags")
    public String gotoTag() {
        return "adminLayui/tag-list";
    }

    /**
     * @Description: 返回未删除状态下的所有标签
     * @Param: []
     * @return: com.zhulin.blog.dto.Result<com.zhulin.blog.entity.BlogTag>
     * @date: 2019/8/26 10:13
     */
    @ResponseBody
    @GetMapping("/v1/tags/list")
    public ResultBO<List<BlogTagEntity>> tagsList() {
        QueryWrapper<BlogTagEntity> queryWrapper = new QueryWrapper<BlogTagEntity>();
        queryWrapper.lambda().eq(BlogTagEntity::getIsDeleted, DeleteStatusEnum.NO_DELETED.getStatus());
        List<BlogTagEntity> list = blogTagService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return ResultBO.error500();
        }
        return ResultBO.of(list);
    }

    /**
     * 标签分页
     *
     * @param ajaxPutPage
     * @param condition
     * @return com.site.blog.pojo.dto.AjaxResultPage<com.site.blog.entity.BlogTag>
     * @date 2019/9/1 11:20
     */
    @ResponseBody
    @GetMapping("/v1/tags/paging")
    public AjaxResultPage<BlogTagEntity> getCategoryList(AjaxPutPage<BlogTagEntity> ajaxPutPage, BlogTagEntity condition) {
        QueryWrapper<BlogTagEntity> queryWrapper = new QueryWrapper<>(condition);
        queryWrapper.lambda()
                .ne(BlogTagEntity::getTagId, 1);
        Page<BlogTagEntity> page = ajaxPutPage.putPageToPage();
        blogTagService.page(page, queryWrapper);
        AjaxResultPage<BlogTagEntity> result = new AjaxResultPage<>();
        result.setData(page.getRecords());
        result.setCount(page.getTotal());
        return result;
    }

    /**
     * 修改标签状态
     *
     * @param blogTag
     * @return com.site.blog.pojo.dto.Result
     * @date 2019/8/30 14:55
     */
    @ResponseBody
    @PostMapping("/v1/tags/isDel")
    public ResultBO<String> updateCategoryStatus(BlogTagEntity blogTag) {
        boolean flag = blogTagService.updateById(blogTag);
        if (flag) {
            return ResultBO.success();
        }
        return ResultBO.error500();
    }

    /**
     * 添加标签
     *
     * @param blogTag
     * @return com.site.blog.pojo.dto.Result
     * @date 2019/9/2 10:12
     */
    @ResponseBody
    @PostMapping("/v1/tags/add")
    public ResultBO<String> addTag(BlogTagEntity blogTag) {
        blogTag.setCreateTime(new Date());
        boolean flag = blogTagService.save(blogTag);
        if (flag) {
            return ResultBO.success();
        } else {
            return ResultBO.error500();
        }
    }

    /**
     * 清除标签
     *
     * @param tagId
     * @return com.site.blog.pojo.dto.Result
     * @date 2019/9/2 18:41
     */
    @ResponseBody
    @PostMapping("/v1/tags/clear")
    public ResultBO<String> clearTag(Integer tagId) throws RuntimeException {
        if (blogTagService.clearTag(tagId)) {
            return ResultBO.success();
        }
        return ResultBO.error500();
    }

    /**
     * 修改标题名字
     *
     * @return com.site.blog.pojo.dto.Result<java.lang.String>
     * @author Linn-cn
     * @date 2020/9/1
     */
    @ResponseBody
    @PostMapping("/v1/tags/update")
    public ResultBO<String> updateCategory(BlogTagEntity blogTag) {
        // TODO blogInfo的tags无实际意义，所以这里不再修改冗余的tags。
        BlogTagEntity sqlBlogTag = blogTagService.getById(blogTag.getTagId());
        boolean flag = sqlBlogTag.getTagName().equals(blogTag.getTagName());
        if (!flag) {
            blogTagService.updateById(blogTag);
        }
        return ResultBO.success();
    }
}
